const mongoose = require('mongoose');

const groupSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true,
    maxlength: 50
  },
  activityId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Activity',
    required: true
  },
  members: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }]
}, {
  timestamps: true
});

// 索引
groupSchema.index({ activityId: 1 });
groupSchema.index({ members: 1 });

// 中间件：在保存前设置组名
groupSchema.pre('save', function(next) {
  if (!this.name && this.activityId) {
    // 这里会在后续设置组名
    // 例如：Group 1, Group 2, ...
  }
  next();
});

module.exports = mongoose.model('Group', groupSchema);